package com.jwmp.rbac.service.impl;

import com.jwmp.core.util.PageResult;
import com.jwmp.core.util.QueryObject;
import com.jwmp.rbac.domain.Role;
import com.jwmp.rbac.mapper.RoleMapper;
import com.jwmp.rbac.service.IRoleService;
import com.jwmp.core.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0
 */
public class RoleServiceImpl implements IRoleService {

    public void insert(Role role) {
        SqlSession session = MyBatisUtil.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        roleMapper.insert(role);
        session.commit();
        session.close();
    }

    public void update(Role role) {
        SqlSession session = MyBatisUtil.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        roleMapper.update(role);
        session.commit();
        session.close();
    }

    public void delete(Long roleId) {
        SqlSession session = MyBatisUtil.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        roleMapper.delete(roleId);
        session.commit();
        session.close();
    }

    public PageResult queryList(QueryObject qo) {
        SqlSession session = MyBatisUtil.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        int totalCount = roleMapper.selectCount(qo);
        if (totalCount <= 0) {
            session.close();
            return new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }

        List<Role> roleList = roleMapper.selectList(qo);
        session.close();
        return new PageResult(qo.getCurrentPage(),qo.getPageSize(),roleList,totalCount);
    }

    public Role selectOne(Long id) {
        SqlSession session = MyBatisUtil.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        Role role = roleMapper.selectOne(id);
        session.close();
        return role;
    }
}
